home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 453 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.0 KB  |  103 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: tim@franck.Princeton.EDU (Tim Hollebeek)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: typedef not strong
  5. Date: 22 Feb 1996 15:53:02 PST
  6. Organization: Princeton University
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4gitti$ken@cnn.Princeton.EDU>
  9. References: <4g5sm4$dtt@natasha.rmii.com> <4g8vdo$igt@mulga.cs.mu.OZ.AU> <4gimon$317@clarknet.clark.net>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 22 Feb 1996 23:23:30 GMT
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMS0B9Ey4NqrwXLNJAQFLewH+Mk82irh9uI9JSIPmC7yhg8nMZS5AQw2U
  15.     nL6PYwP3HMxMOmD/AiPQIV1RfAatxRr52bPUVZdxWkxTpB4i1uh4YA==
  16.     =kifU
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Harlan Messinger (gusty@clark.net) wrote:
  20. : Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
  21. : : rpayne@rainbow.rmii.com (Robert Payne) writes:
  22. : : 
  23. : : >Why has C++ stayed with the weak typedef?  It has always seemed to
  24. : : >me that it should provide a new type and not just a synomym.  Some
  25. : : >lints check for strong typing but I haven't found a compiler that 
  26. : : >will enforce it.  I know this must have been debated at some point
  27. : : >but I didn't find it in a FAQ.  Could someone please enlighten me?
  28. : : 
  29. : : If typedef were to create a new type, what operations would be allowed
  30. : : on the new type?
  31. : : 
  32. : : I think that changing the meaning of `typedef' would be a bad idea.
  33. : : It might make sense to propose a new construct, say `newtypedef', that
  34. : : did something different, but C++ already has a way to create new types
  35. : : (`class'), and it is not at all clear that adding a new way would be
  36. : : worth the additional complexity.
  37.  
  38. : I see two issues at odds here, and a desire to satisfy them both with one 
  39. : construct.
  40.  
  41. : One issue is type safety in function calls. You might have
  42.  
  43. :     typedef double DOLLARS;
  44. :     typedef double INTEREST_RATE;
  45.  
  46. : and want to define a function declared as
  47.  
  48. :     DOLLARS PresentValue(DOLLARS futureValue, 
  49. :         INTEREST_RATE rate, int YEARS);
  50.  
  51. : that will keep the user from accidentally swapping the money and interest 
  52. : arguments when coding a call to the function.
  53.  
  54. : On the other hand, within functions that use these types, you might want
  55. : all the ordinary mathematical functions to apply as usual to the raw
  56. : values underneath. The function call has already guaranteed that the set
  57. : of arguments given has types that are meaningful given the function's 
  58. : purpose. Now we want to take these arguments for what they really 
  59. : are--doubles--and apply normal arithmetic to them with no headaches.
  60.  
  61. Wow, I did just exactly this just yesterday.  I was having problems
  62. keeping track of what numbers were in internal format, and which were
  63. in external format, so I made two classes:
  64.  
  65. class externalValue {
  66.     double v;
  67. public:
  68.     operator double() { return v; }
  69.     explicit externalValue(double x) { v = x; }
  70.     /* extra fns to read/write to files */
  71. };
  72.  
  73. class internalValue {
  74.     double v;
  75. public:
  76.     operator double() { return v; }
  77.     explicit internalValue(double x) { v = x; }
  78. };
  79.  
  80. This way functions and classes can define variables as external or internal,
  81. but complicated formulas involving the numbers still work fine.
  82.  
  83. I then have a series of conversion functions:
  84.  
  85. internalValue toInternal(externalValue v) {
  86.     return internalValue(3 * v - 4);
  87. }
  88.  
  89. etc.  Works quite nicely.
  90.  
  91. ---------------------------------------------------------------------------
  92. Tim Hollebeek         | Disclaimer :=> Everything above is a true statement,
  93. Electron Psychologist |                for sufficiently false values of true.
  94. Princeton University  | email: tim@wfn-shop.princeton.edu
  95. ----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)
  96. ---
  97. [ To submit articles: Try just posting with your newsreader.  If that fails,
  98.                       use mailto:std-c++@ncar.ucar.edu
  99.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  100.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  101.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  102. ]
  103.